﻿using ChartDirector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi
{
    class DrawContour
    {
        public static WinChartViewer Draw(ReadInTemp ReadInData)
        {
            WinChartViewer chartViewer1 = new MyWinChartViewer();

            createChart(chartViewer1, 2, ReadInData);

            chartViewer1.Invalidate();

            return chartViewer1;
        }

        public static double[] colors = { 0x006837, 0x1a9850, 0x66bd63, 0xa6d96a, 0xd9ef8b, 0xffffbf, 0xfee08b, 0xfdae61, 0xf46d43, 0xd73027, 0xa50026 };

        public static void createChart(WinChartViewer viewer, int chartIndex, ReadInTemp readIn)
        {
            // The x and y coordinates of the grid
            double[] dataX = new double[80];
            double[] dataY = new double[80];
            for (int x = 0; x < 80; x++)
            {
                dataX[x] = x;
                dataY[x] = x;
            }


            // Use random numbers for the z values on the XY grid
            //RanSeries r = new RanSeries(99);
            double[] dataZ = readIn.RawDataArray;// r.get2DSeries(dataX.Length, dataY.Length, -0.9, 0.9);

            // Create a XYChart object of size 420 x 360 pixels
            XYChart c = new XYChart(700, 600);

            // Set the plotarea at (30, 25) and of size 300 x 300 pixels. Use semi-transparent grey
            // (0xdd000000) horizontal and vertical grid lines
            c.setPlotArea(1,0, 500, 500, -1, -1, -1, unchecked((int)0xdd000000), -1);

            // Set the x-axis and y-axis scale
            c.xAxis().setLinearScale(0, 79, 1);
            c.yAxis().setLinearScale(0, 79, 1);

            // Add a contour layer using the given data
            ContourLayer layer = c.addContourLayer(dataX, dataY, dataZ);

            // Enable contour label by setting its style to black (000000) 8pt default bold font
            layer.setContourLabelStyle("bold", 8, 0x000000);

            // Set the label to display the value in a CDML block with semi-transparent white
            // (4FFFFFFF) background and 2 pixels left/right margin and 1 pixel top/bottom margin
            layer.setContourLabelFormat("<*block,bgcolor=4FFFFFFF,margin=2 2 1 1*>{value}");

            // Move the grid lines in front of the contour layer
            c.getPlotArea().moveGridBefore(layer);

            // Add a color axis (the legend) in which the top left corner is anchored at (350, 25).
            // Set the length to 400 300 and the labels on the right side.
            ColorAxis cAxis = layer.setColorAxis(515, 10, Chart.TopLeft, 400, Chart.Right);

            //ColorAxis cAxis = layer.setColorAxis(505, 40, Chart.TopLeft, 400, Chart.Right);

            if (chartIndex == 1)
            {
                // Speicify a color gradient as a list of colors, and use it in the color axis.
                int[] colorGradient = { 0x0000cc, 0xffffff, 0x00aa00 };
                cAxis.setColorGradient(true, colorGradient);
            }
            else if (chartIndex == 2)
            {
                // Specify the color scale to use in the color axis
                double[] colorScale = {-1.0, 0x1a9850, -0.75, 0x66bd63, -0.5, 0xa6d96a, -0.25,
                    0xd9ef8b, 0, 0xfee08b, 0.25, 0xfdae61, 0.5, 0xf46d43, 0.75, 0xd73027, 1};


                List<double> colorScaleList = new List<double>();


                double BandWidth = (readIn.Max - readIn.Min) / 10.0;


                colorScaleList.Add(readIn.Min);
                colorScaleList.Add(colors[0]);

                int index = 1;
                for (; index <= 9; index++)
                {
                    double value = readIn.Min + index * BandWidth;

                    colorScaleList.Add(value);

                    colorScaleList.Add(colors[index]);


                }
                colorScaleList.Add(readIn.Max);
                colorScaleList.Add(colors[index]);



                cAxis.setColorScale(colorScaleList.ToArray(),-1, -1);
            }
            else if (chartIndex == 3)
            {
                // Specify the color scale to use in the color axis. Also specify an underflow color
                // 0x66ccff (blue) for regions that fall below the lower axis limit.
                double[] colorScale = {0, 0xffff99, 0.2, 0x80cdc1, 0.4, 0x35978f, 0.6, 0x01665e,
                    0.8, 0x003c30, 100.0, 0x003c50 };
                cAxis.setColorScale(colorScale, 0x66ccff, 0x0000ff);
            }

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "",
                "title='<*cdml*><*font=bold*>X: {x|2}<*br*>Y: {y|2}<*br*>Z: {z|2}'");
        }
    }
}
